<?php 
class Kardex extends AppModel{
    var $name = 'Kardex';
    var $useTable = 'kardexes';
    var $validate = array(
		'id'=>array('notEmpty'=>array('rule'=>'notEmpty','last'=>true,'menssage'=>'Verifique los datos enviados')),
		'documento_id'=>array('notEmpty'=>array('rule'=>'notEmpty','last'=>true,'menssage'=>'Documento: Verifique los datos enviados')),
		'almacenes_articulos_id'=>array('notEmpty'=>array('rule'=>'notEmpty','last'=>true,'menssage'=>'Alm_Art: Verifique los datos enviados')),
		'almacen_stock'=>array('numeric'=>array('rule' =>'numeric','last'=>true,'message'=>'Alm Stock Fisico: El ingreso es solo de numeros')),
    	'articulo_stock'=>array('numeric'=>array('rule' =>'numeric','last'=>true,'message'=>'Art Stock Fisico: El ingreso es solo de numeros'))
	);
	function visualizarError($error){if(empty($error)){	return 	array('respuesta'=>true, 'mensaje'=>"El registro a sido actualizado");}	foreach($error as $value){ return array('respuesta'=>false, 'mensaje'=>$value);}}	
	
    function getDatosArticulo($data){
        $qry = "select 
        			Documento.id AS \"Documento__id\",
        			Documento.created::date AS \"Documento__fecha\",
                	Documento.numero_serie AS \"Documento__numero\",
                	Tipodocumento.descripcion AS \"Documento__descripcion\",
                	Kardex.ingreso AS \"Documento__ingreso\",
                	Kardex.salida AS \"Documento__salida\",
                	kardex.almacen_stock AS \"Documento__almacen_stock\",
                	kardex.articulo_stock AS \"Documento__articulo_stock\",
                	Tipodocumento.modelo AS \"Documento__modelo\"
                from kardexes AS Kardex
                	JOIN almacenes_articulos AS AlmacenesArticulo ON AlmacenesArticulo.id = Kardex.almacenes_articulos_id
                	JOIN articulos AS Articulo ON Articulo.id = AlmacenesArticulo.articulo_id
                	JOIN documentos AS Documento ON Documento.id = Kardex.documento_id
                	JOIN tipodocumentos AS Tipodocumento ON Tipodocumento.id = Documento.tipodocumento_id
                where
                	Documento.created::date between '".$data['Articulo']['fecha_de']."' and '".$data['Articulo']['fecha_a']."'
                	and AlmacenesArticulo.articulo_id = ".$data['Articulo']['id'];
         
        if(empty($data['Articulo']['almacen_id'])){
            $qry .= " order by Kardex.id asc"; 
    	}else{
    	    $qry .= " and AlmacenesArticulo.almacen_id = ".$data['Articulo']['almacen_id']."
    	    		  order by Kardex.id asc";
    	}
                	
        $query = $this->query($qry);
        
        if(!empty($query)){
            if(empty($data['Articulo']['almacen_id'])){
                $stockAnterior = $query[0]['Documento']['articulo_stock'] - $query[0]['Documento']['ingreso'] + $query[0]['Documento']['salida'];
        	}else{
        	    $stockAnterior = $query[0]['Documento']['almacen_stock'] - $query[0]['Documento']['ingreso'] + $query[0]['Documento']['salida'];
        	}
        }
        if(empty($stockAnterior)) $stockAnterior = 0;
        return array('principal'=>$query,'stockAnterior'=>$stockAnterior);
    }
    
    
/*
 select 
                	Documento.created::date AS \"Documento__fecha\",
                	Documento.numero_serie AS \"Documento__numero\",
                	Tipodocumento.descripcion AS \"Documento__descripcion\",
                	Kardex.ingreso AS \"Documento__ingreso\",
                	Kardex.salida AS \"Documento__salida\",
                	kardex.almacen_stock AS \"Documento__almacen_stock\",
                	kardex.articulo_stock AS \"Documento__articulo_stock\",
                	Tipodocumento.modelo AS \"Documento__modelo\"
                from kardexes AS Kardex
                	JOIN almacenes_articulos AS AlmacenesArticulo ON AlmacenesArticulo.id = Kardex.almacenes_articulos_id
                	JOIN articulos AS Articulo ON Articulo.id = AlmacenesArticulo.articulo_id
                	JOIN documentos AS Documento ON Documento.id = Kardex.documento_id
                	JOIN tipodocumentos AS Tipodocumento ON Tipodocumento.id = Documento.tipodocumento_id
                where
                	Documento.created::date between '23/01/2011' and '28/02/2011'
                	and AlmacenesArticulo.articulo_id = 1
                	and AlmacenesArticulo.almacen_id = 1
                order by Kardex.id asc
 */
}
